package com.encryption.feature.doc.ui.render
{
	import com.encryption.controls.loading.DelayPopUpManager;
	import com.encryption.controls.tip.TipShowManager;
	import com.encryption.controls.util.FileDownLoad;
	import com.encryption.feature.doc.FileLogManager;
	import com.encryption.feature.doc.ui.component.FileDownload;
	import com.encryption.framework.constant.FileActionConstant;
	import com.encryption.rpc.model.File;
	
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;

	public class DownloadOperationLabel extends OperationLabel
	{
		
		private var downloadURL:URLRequest;
		private var fileReference:FileReference; //这是要主要的地方
		
		public function DownloadOperationLabel()
		{
			super();
			
			text = "下载";
		}
		
		override protected function get field():String
		{
			return "canDownload";
		}
		
		override protected function mouseClickHandle(event:MouseEvent):void
		{
//			var fileDownload:FileDownload = new FileDownload();
//			fileDownload.load(file.fileUrl, "");
//			fileDownload.download();
			
			download();
		}
		
		
		public function download():void {
			var file:File = File(target);
			downloadURL = new URLRequest();
			var encryUrl:String = file.encryptionUrl;
			downloadURL.url = getUrlPath() + encryUrl;
			var fileName:String = file.name;
			var suffix:String = encryUrl.substring(encryUrl.lastIndexOf("."));
			fileReference = new FileReference();
			configureListeners(fileReference);
			fileReference.download(downloadURL, fileName + suffix);
		}
		
		private function getUrlPath():String
		{
			var url:String = FlexGlobals.topLevelApplication.url;
			
			return url.substring(0, url.lastIndexOf("/"));
		}
		
		private function configureListeners(dispatcher:IEventDispatcher):void {
			dispatcher.addEventListener(Event.CANCEL, cancelHandler);
			dispatcher.addEventListener(Event.COMPLETE, completeHandler);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			dispatcher.addEventListener(Event.OPEN, openHandler);
			dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			dispatcher.addEventListener(Event.SELECT, selectHandler);
		}
		
		private function cancelHandler(event:Event):void {
			trace("cancelHandler: " + event);
		}
		
		private function completeHandler(event:Event):void {
			DelayPopUpManager.removeAllTips();
			FileLogManager.instance.addLog(File(target).id, FileActionConstant.ACTION_DOWNLOAD);
			
			TipShowManager.showErrorTip("下载成功");
			
			trace("completeHandler: " + event);
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void {
			DelayPopUpManager.removeAllTips();
			TipShowManager.showErrorTip("网络异常，下载失败");
			trace("ioErrorHandler: " + event);
		}
		
		private function openHandler(event:Event):void {
			trace("openHandler: " + event);
		}
		
		private function progressHandler(event:ProgressEvent):void {
			DelayPopUpManager.popUp("下载中", FlexGlobals.topLevelApplication as UIComponent);
			
//			var fileReference:FileReference = FileReference(event.target);
			trace("progressHandler name=" + fileReference.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void {
			trace("securityErrorHandler: " + event);
		}
		
		private function selectHandler(event:Event):void {
			var fileReference:FileReference = FileReference(event.target);
			trace("selectHandler: name=" + fileReference.name + " URL=" + downloadURL.url);
		}
	}
}